package com.zerui.manager.app.accessauthorization.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.cola.dto.PageResponse;
import com.zerui.manager.accessauthorization.AccessAuth;
import com.zerui.manager.accessauthorization.domainservice.AccessAuthRepository;
import com.zerui.manager.web.cmd.AccessAuthQryCmd;
import com.zerui.manager.web.vo.AccessAuthPageVO;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liushaohua
 * @title: AccessAuthQryCmdExe
 * @description:
 * @date 2023/8/28 17:51
 */
@Component
@CatchAndLog
public class AccessAuthQryCmdExe {

	@Resource
	private AccessAuthRepository accessAuthRepository;
	public PageResponse<AccessAuthPageVO> execute(AccessAuthQryCmd accessAuthQryCmd) {
		Page<AccessAuth> pageResponse = accessAuthRepository.getPageList(accessAuthQryCmd);

		List<AccessAuth> records = pageResponse.getRecords();
		List<AccessAuthPageVO> authPageVOList = new ArrayList<>(16);
		if(CollectionUtils.isNotEmpty(records)){
			records.forEach(e-> authPageVOList.add(AccessAuthPageVO.builder()
					.name(e.getName())
					.remark(e.getRemark())
					.createTime(e.getCreateTime())
					.id(e.getId())
					.date(e.getDateType().equals(1) ? createDate(e.getStartDate(),e.getEndDate()) :"始终有效" )
					.time(e.getTimeType().equals(1) ? createTime(e.getStartTime(),e.getEndTime()) : "全体有效")
					.dateType(e.getDateType())
					.timeType(e.getTimeType())
					.build()));
		}
		return PageResponse.of(authPageVOList,(int)pageResponse.getTotal(),
				(int) pageResponse.getSize(),
				(int) pageResponse.getCurrent());
	}

	private String createTime(LocalTime startTime, LocalTime endTime) {
		StringBuilder builder = new StringBuilder();
		String start_time = startTime.format(DateTimeFormatter.ofPattern("HH:mm"));
		String end_time = endTime.format(DateTimeFormatter.ofPattern("HH:mm"));
		return builder.append(start_time).append("-").append(end_time).toString();
	}

	private String createDate(LocalDate startDate, LocalDate endDate) {
		StringBuilder builder = new StringBuilder();
		String start_date = startDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
		String end_date = endDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
		return builder.append(start_date).append("-").append(end_date).toString();
	}
}
